#include <stdio.h>
#include <WinSock2.h>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>



#pragma comment(lib,"ws2_32.lib")
void clearScreen() {
	system("cls");  // Windows
	// system("clear"); // Unix/Linux
}
char toUpperCase(char c) {
	return std::toupper(static_cast<unsigned char>(c));
}
using namespace std;
void UserLogin(SOCKET clntSock);
void StudentOperation();
void TeacherOperation();
void UserEnroll(SOCKET clntSock);
void SearchMessage(SOCKET clntSock);
void MessageChange(string UserAccount, SOCKET clntSock);
void StudentidChange(string UserAccount, SOCKET clntSock);
void PasswordChange(string UserAccount, SOCKET clntSock);
void StudentDelete(SOCKET clntSock);
void Test(SOCKET clntSock);
void TestBegan(SOCKET clntSock);
void CreateQue(SOCKET clntSock);
void QueSearch(SOCKET clntSock);
void QueDelete(SOCKET clntSock);



void sql_panduan(SOCKET clntSock);
void id(string ID, SOCKET clntSoc);
void Name(string name, SOCKET clntSock);
void Ro(string Role, SOCKET clntSock);


enum choice
{
	login,
	enroll,
	logout
};
enum student_choice
{
	message_change = 1,
	text_parpare,
	student_logout

};
enum teacher_choice
{
	message_change_te = 1,
	search_message,
	s_m_d,
	create_que,
	search_que,
	account_logout


};
enum message
{
	studentID = 1,
	password
};
enum search_way
{
	all,
	student_id,
	account,
	role

};

void main()
{


	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	//创建套接字
	SOCKET clntSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

	//向服务器发送消息
	sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));			//每个字节都用0填充
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	sockAddr.sin_port = htons(8888);
	connect(clntSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));

	int operation;

	while (1)
	{
		printf("请选择操作（0.登录,1.注册,2.退出）:");
		cin >> operation;
		switch (choice(operation))
		{
		case login: UserLogin(clntSock);
			break;
		case enroll:UserEnroll(clntSock);
			break;
		case logout:return;
		default:printf("操作有误,请重新选择");
			break;
		}
	}
	printf("结束");

}

void UserLogin(SOCKET clntSock)
{
	const char* Login = "登录操作";
	send(clntSock, Login, strlen(Login) + sizeof(char), NULL);


	string UserAccount, UserPasswd;
	cout << "请输入用户账号：";
	cin >> UserAccount;
	const char* account = UserAccount.c_str();
	send(clntSock, account, strlen(account) + sizeof(char), NULL);
	cout << "请输入用户密码：";
	cin >> UserPasswd;
	const char* passwd = UserPasswd.c_str();
	send(clntSock, passwd, strlen(passwd) + sizeof(char), NULL);
	//服务端验证
	//返回结果
	char szBuffer[MAXBYTE] = { 0 };
	recv(clntSock, szBuffer, MAXBYTE, NULL);
	cout << szBuffer << endl;
	if (strcmp(szBuffer, "登录成功") == 0)
	{
		clearScreen();
		char role[MAXBYTE] = { 0 };
		recv(clntSock, role, MAXBYTE, NULL);
		if (strcmp(role, "st") == 0)
		{

			cout << "欢迎同学" << endl;
			while (1)
			{

				StudentOperation();
				string student_operation;
				cin >> student_operation;
				const char* str1 = student_operation.c_str();
				send(clntSock, str1, strlen(str1) + sizeof(char), NULL);
				switch (student_choice(stoi(student_operation)))
				{

				case message_change:MessageChange(UserAccount, clntSock);
					break;
				case text_parpare:Test(clntSock);
					break;
				case student_logout:return;
				default:printf("操作有误");
					break;
				}
			}
		}
		else
		{
			cout << "欢迎老师" << endl;
			while (1)
			{
				TeacherOperation();
				string teacher_operation;
				cin >> teacher_operation;
				const char* str1 = teacher_operation.c_str();
				send(clntSock, str1, strlen(str1) + sizeof(char), NULL);
				switch (teacher_choice(stoi(teacher_operation)))
				{
				case message_change_te:MessageChange(UserAccount, clntSock);
					break;
				case search_message:SearchMessage(clntSock);
					break;
				case s_m_d:StudentDelete(clntSock);
					break;
				case create_que:CreateQue(clntSock);
					break;
				case search_que:QueSearch(clntSock);
					break;
				case account_logout:return;
				default:cout << "操作有误,请重新输入" << endl;
					break;
				}
			}

		}
	}
	else
	{
		cout << "请重新登录" << endl;
	}


}


void StudentOperation()

{
	cout << "请输入以下数字来执行操作" << endl;
	cout << "1.修改信息" << endl;
	cout << "2.准备考试" << endl;
	cout << "3.退出" << endl;


}
void TeacherOperation()
{
	cout << "请输入以下数字来执行操作" << endl;
	cout << "1.修改信息" << endl;
	cout << "2.查询信息" << endl;
	cout << "3.删除学生信息" << endl;
	cout << "4.出题" << endl;
	cout << "5.查题" << endl;
	cout << "6.退出" << endl;
	cout << "请输入要执行的操作:";
}
void UserEnroll(SOCKET clntSock)
{
	const char* Enroll = "注册操作";
	send(clntSock, Enroll, strlen(Enroll) + sizeof(char), NULL);
	string cid, cname, cpwd, crol;
	cout << "请输入学号:";//将这些信息输出显示；
	cin >> cid;//键盘输入信息；
	const char* id = cid.c_str();
	send(clntSock, id, strlen(id) + sizeof(char), NULL);

	cout << "请输入要创建的账号:";
	cin >> cname;
	const char* name = cname.c_str();
	send(clntSock, name, strlen(name) + sizeof(char), NULL);

	cout << "请输入账号的密码:";
	cin >> cpwd;
	const char* pwd = cpwd.c_str();
	send(clntSock, pwd, strlen(pwd) + sizeof(char), NULL);


	cout << "请输入你的身份(te/st):";
	cin >> crol;
	const char* role = crol.c_str();
	send(clntSock, role, strlen(role) + sizeof(char), NULL);

	bool tem = false;

	if (crol == "te" || "st")
	{
		tem = true;
	}
	while (tem = false)
	{
		cout << "无此类身份,请再次输入(te/st):";
		cin >> crol;
		const char* role = crol.c_str();
		send(clntSock, role, strlen(role) + sizeof(char), NULL);
		if (crol == "te" || "st")
		{
			break;
		}
	}
	char yanzheng[MAXBYTE] = { 0 };
	recv(clntSock, yanzheng, MAXBYTE, NULL);
	if (strcmp(yanzheng, "创建失败") == 0)
	{
		printf("创建失败\n");

	}
	else
	{
		printf("创建成功\n");
	}
}
void MessageChange(string UserAccount, SOCKET clntSock)
{
	cout << "请输入要修改的内容(1.学号,2.密码):";
	string choice;
	cin >> choice;
	const char* Op = choice.c_str();
	send(clntSock, Op, strlen(Op) + sizeof(char), NULL);
	string  studentid;
	switch (message(stoi(choice)))
	{
	case studentID:StudentidChange(UserAccount, clntSock);
		break;
	case password:PasswordChange(UserAccount, clntSock);
		break;
	default:printf("操作有误,请重新输入");
		break;
	}


}
void PasswordChange(string UserAccount, SOCKET clntSock)
{
	string UserPassword1, UserPassword2;
	cout << "请输入修改后的用户密码：";
	cin >> UserPassword1;
	const char* passwd1 = UserPassword1.c_str();
	send(clntSock, passwd1, strlen(passwd1) + sizeof(char), NULL);
	cout << "请确认密码：";
	cin >> UserPassword2;
	const char* passwd2 = UserPassword2.c_str();
	send(clntSock, passwd2, strlen(passwd2) + sizeof(char), NULL);
	while (UserPassword2 != UserPassword1)
	{
		cout << "两次密码输入不相同，请重新输入";
		cout << "请输入修改后的用户密码：" << endl;
		cin >> UserPassword1;
		const char* passwd1 = UserPassword1.c_str();
		send(clntSock, passwd1, strlen(passwd1) + sizeof(char), NULL);
		cout << "请确认密码：" << endl;
		cin >> UserPassword2;
		const char* passwd2 = UserPassword2.c_str();
		send(clntSock, passwd2, strlen(passwd2) + sizeof(char), NULL);
	}
	sql_panduan(clntSock);




}
void sql_panduan(SOCKET clntSock)
{

	char yanzheng[MAXBYTE] = { 0 };
	recv(clntSock, yanzheng, MAXBYTE, NULL);
	if (strcmp(yanzheng, "修改失败") == 0)
	{
		cout << yanzheng << endl;

	}
	else
	{
		cout << yanzheng << endl;
	}
}
void StudentidChange(string UserAccount, SOCKET clntSock)
{
	string studengtid;
	cout << "请输入修改后的学号：";
	cin >> studengtid;
	const char* id = studengtid.c_str();
	send(clntSock, id, strlen(id) + sizeof(char), NULL);
	sql_panduan(clntSock);
}
void SearchMessage(SOCKET clntSock)
{
	cout << "请输入查询方式(0.查询全部,1.学号查询,2.账户查询,3.角色查询):";
	string s, ID, name, Role;//查询方式
	string search_operation;//查询操作
	cin >> search_operation;
	const char* Op = search_operation.c_str();
	send(clntSock, Op, strlen(Op) + sizeof(char), NULL);
	switch (search_way(stoi(search_operation)))
	{
	case all:s = "select* from message;";
		break;
	case student_id:
		cout << "请输入学号:";
		cin >> ID;
		id(ID, clntSock);
		break;
	case account:
		cout << "请输入账户:";
		cin >> name;
		Name(name, clntSock);
		break;
	case role:
		cout << "请输入角色(st/te):";
		cin >> Role;
		Ro(Role, clntSock);
		break;
	default:printf("操作有误");
		break;
	}


	char szBuffer[MAXBYTE] = { 0 };
	recv(clntSock, szBuffer, MAXBYTE, NULL);
	if (strcmp(szBuffer, "登录成功") == 0)
	{

		printf("查询的用户存在\n");
		//printf("%s\n", ss);
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");
		//显示数据
		//给ROW赋值，判断ROW是否为空，不为空就打印数据。

		do
		{


			char row0[MAXBYTE] = { 0 };
			recv(clntSock, row0, MAXBYTE, NULL);
			if (strcmp(row0, "完成") == 0)
			{
				break;
			}
			char row1[MAXBYTE] = { 0 };
			recv(clntSock, row1, MAXBYTE, NULL);
			char row2[MAXBYTE] = { 0 };
			recv(clntSock, row2, MAXBYTE, NULL);
			char row3[MAXBYTE] = { 0 };
			recv(clntSock, row3, MAXBYTE, NULL);
			char row4[MAXBYTE] = { 0 };
			recv(clntSock, row4, MAXBYTE, NULL);

			cout << left << "      " << setw(9) << row0 << "|     "
				<< setw(15) << row1 << "|     "
				<< setw(9) << row2 << "|     "
				<< setw(9) << row3 << "|     "
				<< setw(9) << row4 << endl;
			const char* delay = "延迟";
			send(clntSock, delay, strlen(delay) + sizeof(char), NULL);

		} while (1);
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");
		cout << "本次查询结束" << endl;

	}
	else
	{
		printf("用户不存在\n");
	}

}
void StudentDelete(SOCKET clntSock)
{
	cout << "请输入删除方式(1.学号查询,2.账户查询,3.取消删除):";
	string s, ID, name, s1;//查询方式
	string search_operation;//查询操作
	cin >> search_operation;
	const char* Op = search_operation.c_str();
	send(clntSock, Op, strlen(Op) + sizeof(char), NULL);
	switch (search_way(stoi(search_operation)))
	{
	case student_id:
		cout << "请输入学号:";
		cin >> ID;
		id(ID, clntSock);
		break;
	case account:
		cout << "请输入账户:";
		cin >> name;
		Name(name, clntSock);
		break;
	case 3:return;
	default:printf("操作有误");
		break;
	}
	char szBuffer[MAXBYTE] = { 0 };
	recv(clntSock, szBuffer, MAXBYTE, NULL);
	if (strcmp(szBuffer, "登录成功") == 0)
	{

		printf("查询的用户存在\n");
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");

		do
		{
			char row0[MAXBYTE] = { 0 };
			recv(clntSock, row0, MAXBYTE, NULL);
			if (strcmp(row0, "完成") == 0)
			{
				break;
			}
			char row1[MAXBYTE] = { 0 };
			recv(clntSock, row1, MAXBYTE, NULL);
			char row2[MAXBYTE] = { 0 };
			recv(clntSock, row2, MAXBYTE, NULL);
			char row3[MAXBYTE] = { 0 };
			recv(clntSock, row3, MAXBYTE, NULL);
			char row4[MAXBYTE] = { 0 };
			recv(clntSock, row4, MAXBYTE, NULL);

			cout << left << "      " << setw(9) << row0 << "|     "
				<< setw(15) << row1 << "|     "
				<< setw(9) << row2 << "|     "
				<< setw(9) << row3 << "|     "
				<< setw(9) << row4 << endl;
			//i++;

		} while (1);
		for (int i = 0; i < 9 + 15 + 9 + 9 + 9 + 25; i++)
		{
			printf("-");
		}printf("\n");

		string sure;
		cout << "确认删除(Y/N)?:";
		cin >> sure;
		const char* Sure = sure.c_str();
		send(clntSock, Sure, strlen(Sure) + sizeof(char), NULL);
		if (sure == "Y")
		{

			cout << "删除成功" << endl;


		}

		else if (sure == "N")

		{
			cout << "删除取消" << endl;
		}
		else
		{
			cout << "删除失败" << endl;
		}
	}
	else
	{
		cout << "查无此人" << endl;
	}

}
void Test(SOCKET clntSock)
{
	string sure;
	cout << "Are you ready(Y/N)：";
	cin >> sure;
	const char* Sure = sure.c_str();
	send(clntSock, Sure, strlen(Sure) + sizeof(char), NULL);
	if (sure == "Y")
	{
		TestBegan(clntSock);

	}
	else
	{
		cout << "考试已取消" << endl;
		return;
	}


}
void TestBegan(SOCKET clntSock)
{
	const int QuNumber = 3;
	int Score = 0, Number = 0;
	int i = 1;
	string stu_choice;
	string StuChoice[QuNumber];
	string AnswerChoice[QuNumber];
	int score[QuNumber];
	do
	{
		char row1[MAXBYTE] = { 0 };
		recv(clntSock, row1, MAXBYTE, NULL);
		if (strcmp(row1, "完成") == 0)
		{
			break;
		}
		char row2[MAXBYTE] = { 0 };
		recv(clntSock, row2, MAXBYTE, NULL);
		char row3[MAXBYTE] = { 0 };
		recv(clntSock, row3, MAXBYTE, NULL);
		char row4[MAXBYTE] = { 0 };
		recv(clntSock, row4, MAXBYTE, NULL);
		char row5[MAXBYTE] = { 0 };
		recv(clntSock, row5, MAXBYTE, NULL);

		cout << left << i << ". "
			<< setw(25) << row1 << "A:"
			<< setw(9) << row2 << "B:"
			<< setw(9) << row3 << "C:"
			<< setw(9) << row4 << "D:"
			<< setw(9) << row5 << endl << "请选择(A,B,C,D):";
		//int answer= row[6]
		cin >> stu_choice;
		const char* choice = stu_choice.c_str();
		send(clntSock, choice, strlen(choice) + sizeof(char), NULL);

		StuChoice[i - 1] = stu_choice;


		char row6[MAXBYTE] = { 0 };
		recv(clntSock, row6, MAXBYTE, NULL);
		AnswerChoice[i - 1] = row6;

		char row7[MAXBYTE] = { 0 };
		recv(clntSock, row7, MAXBYTE, NULL);
		score[i - 1] = stoi(row7);
		i++;
	} while (1);
	for (int k = 0; k < i - 1; k++)
	{

		cout << StuChoice[k] << " " << AnswerChoice[k] << endl;
		if (StuChoice[k] == AnswerChoice[k])
		{
			Score += score[k];
			Number++;
		}

	}

	printf("学生共答对了%d题目,得分为%d\n", Number, Score);
	if (Score < 60)
	{
		printf("菜鸡一个,哈哈哈哈哈哈\n");
		printf("\n");
	}
	else if (Score == 100)
	{
		cout << "牛逼" << endl;
		printf("\n");
	}
	{

	}

}
void CreateQue(SOCKET clntSock)
{
	string qid, qname, qa, qb, qc, qd, qans, qscore;

	cout << "请输入题目：" << endl;
	cin >> qname;
	const char* name = qname.c_str();
	send(clntSock, name, strlen(name) + sizeof(char), NULL);

	char yanzheng[MAXBYTE] = { 0 };
	recv(clntSock, yanzheng, MAXBYTE, NULL);

	if (strcmp(yanzheng, "成功") == 0)
	{
		cout << "请输入题号：";
		cin >> qid;
		const char* id = qid.c_str();
		send(clntSock, id, strlen(id) + sizeof(char), NULL);

		cout << "请输入选项A：";
		cin >> qa;
		const char* a = qa.c_str();
		send(clntSock, a, strlen(a) + sizeof(char), NULL);

		cout << "请输入选项B：";
		cin >> qb;
		const char* b = qb.c_str();
		send(clntSock, b, strlen(b) + sizeof(char), NULL);

		cout << "请输入选项C：";
		cin >> qc;
		const char* c = qc.c_str();
		send(clntSock, c, strlen(c) + sizeof(char), NULL);

		cout << "请输入选项D：";
		cin >> qd;
		const char* d = qd.c_str();
		send(clntSock, d, strlen(d) + sizeof(char), NULL);

		cout << "请输入标准答案：";
		cin >> qans;
		const char* ans = qans.c_str();
		send(clntSock, ans, strlen(ans) + sizeof(char), NULL);

		cout << "请输入分值：";
		cin >> qscore;
		const char* score = qscore.c_str();
		send(clntSock, score, strlen(score) + sizeof(char), NULL);


		char en[MAXBYTE] = { 0 };
		recv(clntSock, yanzheng, MAXBYTE, NULL);
		if (strcmp(en, "添加试题失败") == 0)
		{
			cout << "试题添加失败" << endl;
		}
		else
		{
			cout << "试题添加成功" << endl;
		}
	}
	else
	{

		printf("添加的试题和表中的试题有重复的，创建项目不成功\n");
	}
}
void QueSearch(SOCKET clntSock)
{

	for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
	{
		printf("-");
	}printf("\n");
	//显示数据
	//给ROW赋值，判断ROW是否为空，不为空就打印数据。
	while (1)
	{
		char row0[MAXBYTE] = { 0 };
		recv(clntSock, row0, MAXBYTE, NULL);
		if (strcmp(row0, "完成") == 0)
		{
			break;
		}
		char row1[MAXBYTE] = { 0 };
		recv(clntSock, row1, MAXBYTE, NULL);
		char row2[MAXBYTE] = { 0 };
		recv(clntSock, row2, MAXBYTE, NULL);
		char row3[MAXBYTE] = { 0 };
		recv(clntSock, row3, MAXBYTE, NULL);
		char row4[MAXBYTE] = { 0 };
		recv(clntSock, row4, MAXBYTE, NULL);
		char row5[MAXBYTE] = { 0 };
		recv(clntSock, row5, MAXBYTE, NULL);
		char row6[MAXBYTE] = { 0 };
		recv(clntSock, row6, MAXBYTE, NULL);
		char row7[MAXBYTE] = { 0 };
		recv(clntSock, row7, MAXBYTE, NULL);
		cout << left << "      " << setw(9) << row0 << "|     "
			<< setw(35) << row1 << "|     "
			<< setw(9) << row2 << "|     "
			<< setw(9) << row3 << "|     "
			<< setw(9) << row4 << "|     "
			<< setw(9) << row5 << "|     "
			<< setw(9) << row6 << "|     "
			<< setw(9) << row7 << endl;
		const char* delay = "延迟";
		send(clntSock, delay, strlen(delay) + sizeof(char), NULL);
	}
	for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
	{
		printf("-");
	}printf("\n");
	cout << "请选择操作(1.删除/2.取消):";
	string operation;
	cin >> operation;
	const char* Op = operation.c_str();
	send(clntSock, Op, strlen(Op) + sizeof(char), NULL);
	switch (stoi(operation))
	{
	case 1:QueDelete(clntSock);;
		break;
	case 2:return;
		break;
	default:cout << "操作错误" << endl;
		break;
	}



}
void QueDelete(SOCKET clntSock)
{
	cout << "请输入要删除的题目的序号:";
	string id;
	cin >> id;
	const char* ID = id.c_str();
	send(clntSock, ID, strlen(ID) + sizeof(char), NULL);


	char cunzai[MAXBYTE] = { 0 };
	recv(clntSock, cunzai, MAXBYTE, NULL);
	if (strcmp(cunzai, "存在") == 0)
	{

		printf("查询的问题存在\n");
		for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
		{
			printf("-");
		}printf("\n");

		//显示数据
		//给ROW赋值，判断ROW是否为空，不为空就打印数据。
		while (1)
		{
			char row0[MAXBYTE] = { 0 };
			recv(clntSock, row0, MAXBYTE, NULL);
			if (strcmp(row0, "完成") == 0)
			{
				break;
			}
			char row1[MAXBYTE] = { 0 };
			recv(clntSock, row1, MAXBYTE, NULL);
			char row2[MAXBYTE] = { 0 };
			recv(clntSock, row2, MAXBYTE, NULL);
			char row3[MAXBYTE] = { 0 };
			recv(clntSock, row3, MAXBYTE, NULL);
			char row4[MAXBYTE] = { 0 };
			recv(clntSock, row4, MAXBYTE, NULL);
			char row5[MAXBYTE] = { 0 };
			recv(clntSock, row5, MAXBYTE, NULL);
			char row6[MAXBYTE] = { 0 };
			recv(clntSock, row6, MAXBYTE, NULL);
			char row7[MAXBYTE] = { 0 };
			recv(clntSock, row7, MAXBYTE, NULL);
			cout << left << "      " << setw(9) << row0 << "|     "
				<< setw(35) << row1 << "|     "
				<< setw(9) << row2 << "|     "
				<< setw(9) << row3 << "|     "
				<< setw(9) << row4 << "|     "
				<< setw(9) << row5 << "|     "
				<< setw(9) << row6 << "|     "
				<< setw(9) << row7 << endl;
		}
		for (int i = 0; i < 9 + 15 + 25 + 25 + 25 + 25 + 25; i++)
		{
			printf("-");
		}printf("\n");
		string sure;
		cout << "确认删除(Y/N)?:";
		cin >> sure;
		const char* Sure = sure.c_str();
		send(clntSock, Sure, strlen(Sure) + sizeof(char), NULL);
		
		char queding[MAXBYTE] = { 0 };
		recv(clntSock, queding, MAXBYTE, NULL);
		cout << Sure << endl;
		if (strcmp(queding,"成功") == 0)
		{

			cout << "删除成功" << endl;
		}
		else
		{
			cout << "删除取消" << endl;
		}
	}
	else
	{
		cout << "查无此问题" << endl;
	}


}

void id(string ID, SOCKET clntSock)
{

	const char* id = ID.c_str();
	send(clntSock, id, strlen(id) + sizeof(char), NULL);
}
void Name(string name, SOCKET clntSock)
{

	const char* nam = name.c_str();
	send(clntSock, nam, strlen(nam) + sizeof(char), NULL);
}
void Ro(string Role, SOCKET clntSock)
{

	const char* ro = Role.c_str();
	send(clntSock, ro, strlen(ro) + sizeof(char), NULL);
}





